<?php


namespace App\Http\Admin\Controllers;

use Illuminate\Http\Request;
use App\Http\Admin\Controllers\Controller as Controller;
use Illuminate\Support\Facades\DB;

/*
 *  案件表接口
 *  @author 小林《1814992057@qq.com》
 *  @datetime   2019/8/23
*/

class CaseLabelController extends Controller{



    /*
     *  添加标签
     *
     *  @param Request $request
     *  @return json
     *
     * */

    public function add_case_label(Request $request){

        $data=$request->only('title','tid');
//
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,20}$/u',$data['title'])) return ThrowApiExc('20001');
        if(!preg_match('/^[0-9]{1,10}$/',$data['tid'])) return ThrowApiExc('20001');

        $lidRes=DB::table('C_label')->where('id',$data['tid'])->get()->toArray();  //根据lid查找数据库 无限级分类

        $token = getToken($request);
        $uid =Redis::get("code:".$token);//获取操作人id



        if(count($lidRes)==0) return ThrowApiExc('40006');

        if($lidRes[0]->tid==0){
            $data['tid']=$lidRes[0]->id;
        }else{
            $data['tid']=$lidRes[0]->tid.",".$lidRes[0]->id;
        }



        $res = DB::table('C_label')->insertGetId(
            [
                'title'=>$data['title'],
                'tid'=>$data['tid'],
                'uid'=>$uid
            ]
        );


        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => ['id'=>$res]
            ];
        }else{
            return ThrowApiExc('40002');
        }




    }


    /*
     * 查询全部的标签 返回json
     *
     * @return json
     *
     * */


    public function  select_case_label(){

        $data=DB::table('C_label')->orderBy('tid')->get();
//        $newdata=[];
//        foreach ($data as $k=>$v){
//            if($v['tid']==0){
//                $newdata[]=$v;
//                unset($data[$k]);
//                $newdata[$k]['data']=order_label($v['id'],$data);
//            }
//        }
//        $newdata=order_label($data);

        return [
            'status_code'      => 200,
            'message'     =>  '请求成功',
            'data' => $data
        ];

    }



    /*
    * 修改标签的状态和标题
    *
    * @param Request $request
    * @return json
    *
    * */

    public function update_case_label(Request $request,$lid){


        $data=$request->only('title','tid');
//
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,20}$/u',$data['title'])) return ThrowApiExc('20001');
        if(!preg_match('/^[0-9]{1,10}$/',$data['tid'])) return ThrowApiExc('20001');

        $lidRes=DB::table('C_label')->where('id',$data['tid'])->get()->toArray();  //根据lid查找数据库 无限级分类

        if(count($lidRes)==0) return ThrowApiExc('40006');

        if($lidRes[0]->tid==0){
            $data['tid']=$lidRes[0]->id;
        }else{
            $data['tid']=$lidRes[0]->tid.",".$lidRes[0]->id;
        }



        $res = DB::table('C_label')->where('id',$lid)->update(
            [
                'title'=>$data['title'],
                'tid'=>$data['tid']
            ]
        );


        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => ['id'=>$res]  
            ];
        }else{
            return ThrowApiExc('40002');
        }



    }

    /*
    * 删除标签
    *
    * @return json
    * */

    public function delete_case_label($lid){

        if(!preg_match('/^[0-9]{1,10}$/',$lid))  return ThrowApiExc('20003');

        $res=DB::table('C_label')->where('id',$lid)->delete();

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '删除成功',
                'data' => []
            ];
        }else{
            return ThrowApiExc('40003');
        }

    }



}
